﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace RSM.Business
{
    public class FileManager
    {
        public void CreateFolder(string path)
        {
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
        }
        public void GenerateFeatureFile(string contentTypeID, string path, string fileName, string featureName, string desc, string scope)
        {

            string fName = fileName.Replace(".aspx", "");

            XNamespace ns = "http://schemas.microsoft.com/sharepoint/";
            XElement provision = new XElement(ns + "Elements");
            XDocument eleDoc = new XDocument();


            if (File.Exists(path + "ProvisionedFiles.xml"))
            {
                XmlDocument appendDoc = new XmlDocument();
                appendDoc.Load(path + "ProvisionedFiles.xml");

                XmlElement modEle = appendDoc.CreateElement("Module", "http://schemas.microsoft.com/sharepoint/");

                modEle.SetAttribute("Name", "");
                modEle.SetAttribute("Url", "_catalogs/masterpage");
                modEle.SetAttribute("Path", "");
                modEle.SetAttribute("RootWebOnly", "TRUE");

                XmlElement fileEle = appendDoc.CreateElement("File", "http://schemas.microsoft.com/sharepoint/");
                fileEle.SetAttribute("Url", fileName);
                fileEle.SetAttribute("Type", "GhostableInLibrary");

                XmlElement propEle = appendDoc.CreateElement("Property", "http://schemas.microsoft.com/sharepoint/");
                propEle.SetAttribute("Name", "Name");
                propEle.SetAttribute("Value", fileName);

                XmlElement propEle1 = appendDoc.CreateElement("Property", "http://schemas.microsoft.com/sharepoint/");
                propEle1.SetAttribute("Name", "PublishingAssociatedContentType");
                propEle1.SetAttribute("Value", ";#" + fName + ";#" + contentTypeID + ";#");


                fileEle.AppendChild(propEle);
                fileEle.AppendChild(propEle1);

                modEle.AppendChild(fileEle);
                appendDoc.DocumentElement.AppendChild(modEle);
                appendDoc.Save(path + "ProvisionedFiles.xml");

            }
            else
            {
                XElement provisionElements = new XElement(ns + "Module", new XAttribute("Name", ""),
 new XAttribute("Url", "_catalogs/masterpage"), new XAttribute("Path", ""),
 new XAttribute("RootWebOnly", "TRUE"));
                XElement proEle = new XElement(ns + "File", new XAttribute("Url", fileName),
         new XAttribute("Type", "GhostableInLibrary"), new XElement(ns + "Property",
                       new XAttribute("Name", "Name"), new XAttribute("Value", fName)), new XElement(ns + "Property",
                       new XAttribute("Name", "Title"), new XAttribute("Value", fName)), new XElement(ns + "Property",
                       new XAttribute("Name", "PublishingAssociatedContentType"), new XAttribute("Value", ";#" + fName + ";#" + contentTypeID + ";#")));

                provisionElements.Add(proEle);
                provision.Add(provisionElements);
                provision.Save(path + "ProvisionedFiles.xml");
            }
            XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf-16", "true"));
            XElement Elements = new XElement(ns + "Feature", new XAttribute("Id", Guid.NewGuid()),
                new XAttribute("Title", featureName), new XAttribute("Description", desc),
                new XAttribute("Version", "1.0.0.0"), new XAttribute("Hidden", "FALSE"),
                new XAttribute("Scope", scope),
                new XAttribute("DefaultResourceFile", "core")
                );
            XElement moduleEle = new XElement(ns + "ElementManifests", new XElement(ns + "ElementManifest",
                   new XAttribute("Location", "ProvisionedFiles.xml")));
            Elements.Add(moduleEle);
            doc.Add(Elements);
            doc.Save(path + "Feature.xml");
        }
        public void GenerateFeatureFile(string path, string fileName, string featureName, string desc, string scope)
        {
            XNamespace ns = "http://schemas.microsoft.com/sharepoint/";
            XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf-16", "true"));
            XElement Elements = new XElement(ns + "Feature", new XAttribute("Id", Guid.NewGuid()),
                new XAttribute("Title", featureName), new XAttribute("Description", desc),
                new XAttribute("Version", "1.0.0.0"), new XAttribute("Hidden", "FALSE"),
                new XAttribute("Scope", scope),
                new XAttribute("DefaultResourceFile", "core")
                );
            XElement ele = new XElement(ns + "ElementManifests", new XElement(ns + "ElementManifest",
                   new XAttribute("Location", fileName)));
            Elements.Add(ele);
            doc.Add(Elements);
            doc.Save(path + "Feature.xml");
        }
        public void WriteFileOnDisk(string featureTitle, string id, string strFileName, StringBuilder sbPageBuilder)
        {
            string path = @"C://RSMTechnoFiles/" + featureTitle + "/";
            CreateFolder(path);
            strFileName = strFileName + ".aspx";
            FileStream file1 = new FileStream(path + "\\" + strFileName, FileMode.Create);
            StreamWriter SW = new StreamWriter(file1);
            SW.WriteLine(sbPageBuilder);
            SW.Close();
            file1.Close();
            GenerateFeatureFile(id, path, strFileName, featureTitle, "The job of this is to deploy pagelayouts", "Site");
        }

    }

}
